package com.ws.admin.config;

import cn.hutool.core.util.StrUtil;
import com.ws.common.utils.SessionUtils;
import com.ws.system.user.entity.SystemUser;
import com.ws.system.user.service.ISystemUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

public class ShiroRealm extends AuthorizingRealm {

	 /**
     * 获取身份验证信息
     * Shiro中，最终是通过 Realm 来获取应用程序中的用户、角色及权限信息的。
     *
     * @param authenticationToken 用户身份信息 token
     * @return 返回封装了用户信息的 AuthenticationInfo 实例
     */

	 @Autowired
     ISystemUserService userService;

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String userName=token.getUsername();

        /*验证用户名*/
        if(StrUtil.isEmpty(userName)){
            return null;
        }
        SystemUser user=userService.readByUserName(userName);
        if(user==null ||  !StrUtil.equals(user.getUserName(),userName)){
            return null;
        }

        //返回用户信息  会自动判断密码
        return  new SimpleAuthenticationInfo(user.getUserName(), user.getPassword(), getName());
    }

    /**
     * 获取授权信息
     *
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SystemUser user=SessionUtils.get("user");
        String userName = user.getUserName();
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.setRoles(user.getRoles());
        info.setStringPermissions(user.getPermissions());
        return info;
    }
	
}
